package cn.com.homeyoda.aimate.aihealth.ui.health_manage;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.PopularListBean;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentHealthManageBinding;
import cn.com.homeyoda.aimate.aihealth.manager.UserInfoManager;
import cn.com.homeyoda.aimate.aihealth.ui.detail.DetailActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.ChronicDiseaseActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.ChronicDiseaseAddActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.DietPlanActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.SportPlanActivity;
import cn.com.homeyoda.aimate.aihealth.ui.kepu.KePuActivity;

public class HealthManageFragment extends BaseFragment<FragmentHealthManageBinding, HealthManageViewModel> implements View.OnClickListener {

    private HealthManageAdapter healthManageAdapter;
    private List<PopularListBean.RowsBean> kePuList = new ArrayList<>();

    @Override
    public HealthManageViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HealthManageViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getKePuData("", "", "");
        binding.llHealthManageDiet.setOnClickListener(this);
        binding.llHealthManageChronic.setOnClickListener(this);
        binding.llHealthManageSports.setOnClickListener(this);
        binding.tvSeeMore.setOnClickListener(this);
        initRecyclerView();
    }

    private void initRecyclerView() {

        healthManageAdapter = new HealthManageAdapter(R.layout.kepu_normal_item, kePuList, getContext());
        binding.setLayoutManager(new GridLayoutManager(getContext(), 4));
        binding.setAdapter(healthManageAdapter);

        healthManageAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(getContext(), DetailActivity.class);
            intent.putExtra("id", healthManageAdapter.getData().get(position).getId());
            startActivity(intent);
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.kePuData.observe(this, popularListBean -> {
            kePuList = popularListBean;
            UserInfoManager.getInstance().setKePuData(kePuList);
            List<PopularListBean.RowsBean> firstFour = null;
            if (kePuList.size() > 4) {
                firstFour = kePuList.stream()
                        .limit(4)
                        .collect(Collectors.toList());
                healthManageAdapter.setNewData(firstFour);
            } else {
                healthManageAdapter.setNewData(kePuList);
            }

        });

    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_health_manage;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_health_manage_diet) {
            // 饮食计划
            startActivity(new Intent(getContext(), DietPlanActivity.class));
        } else if (id == R.id.ll_health_manage_chronic) {
            startActivity(ChronicDiseaseActivity.class);
        } else if (id == R.id.ll_health_manage_sports) {
            startActivity(new Intent(getContext(), SportPlanActivity.class));
        } else if (id == R.id.tv_see_more) {
            startActivity(new Intent(getActivity(), KePuActivity.class));
        }
    }
}
